local skill = fk.CreateSkill{
  name = "tea__qiaozhu"
}
---@type ActiveSkillSpec
local qiaozhu = {
  anim_type = "support",
  target_num = 0,
  card_num = 1,
  prompt = "#tea__qiaozhu-invoke",
  can_use = function(self, player)
    return player:usedSkillTimes(skill.name, Player.HistoryPhase) == 0 and player:hasSkill(skill.name)
  end,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0 
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    player:showCards(effect.cards)
    local ctype = Fk:getCardById(effect.cards[1]):getTypeString()
    local cards = room:askToCards(player, {
      skill_name = skill.name,
      min_num = 1,
      max_num = 999,
      include_equip = true,
      cancelable = true,
      pattern = ".|.|.|.|.|^"..ctype,
      prompt = "#qiaozhu_recast:::"..ctype,
    })
    if #cards > 0 then
      room:recastCard(cards, player, self.name)
    end
  end,
}
---@type TrigSkelSpec<TrigFunc>
local qiaozhu_trigger = {
  can_refresh = function(self, event, target, player, data)
    if event == fk.GameStart then
      return player:hasSkill("tea__qiaozhu", true)
    elseif event == fk.EventAcquireSkill or event == fk.EventLoseSkill then
      return data == Fk.skills[skill.name] and not table.find(player.room:getOtherPlayers(player), function(p) return p:hasSkill("tea__qiaozhu", true) end)
    else
      return target == player and player:hasSkill("tea__qiaozhu", true, true) and
        not table.find(player.room:getOtherPlayers(player), function(p) return p:hasSkill("tea__qiaozhu", true) end)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.GameStart or event == fk.EventAcquireSkill then
      if player:hasSkill("tea__qiaozhu", true) then
        for _, p in ipairs(room:getOtherPlayers(player)) do
          room:handleAddLoseSkills(p, "tea__qiaozhu&", nil, false, true)
        end
      end
    elseif event == fk.EventLoseSkill or event == fk.Deathed then
      for _, p in ipairs(room:getOtherPlayers(player)) do
        room:handleAddLoseSkills(p, "-tea__qiaozhu&", nil, false, true)
      end
    end
  end,
}
skill:addEffect("active", qiaozhu)
skill:addEffect(fk.GameStart, qiaozhu_trigger)
skill:addEffect(fk.EventAcquireSkill, qiaozhu_trigger)
skill:addEffect(fk.EventLoseSkill, qiaozhu_trigger)
skill:addEffect(fk.Deathed, qiaozhu_trigger)
skill:addTest(function (room, me)
  local comp2 = room.players[2]
  FkTest.setNextReplies(me, { json.encode{
    card = { skill = "tea__qiaozhu&", subcards = { 1 } },
    targets = {}
  } })
  FkTest.setNextReplies(comp2, { json.encode{
    card = { subcards = { 2, 3 } },
    targets = {}
  }, json.encode{
    card = { subcards = { 1 } },
    targets = {}
  } })
  FkTest.runInRoom(function ()
    room:handleAddLoseSkills(comp2, skill.name)
    room:obtainCard(me, 1)
    room:obtainCard(comp2, {2, 3})
    GameEvent.Turn:create(TurnData:new(me, "game_rule", { Player.Play })):exec()
  end)
end)
return skill